import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { getToken, setToken, removeToken } from "@/utils/token";
import { getLogin } from "@/api";

export const useUserStore = defineStore(
  "user",
  {
    persist: true,
    state: () => ({
      token: getToken() || "",
      nickName: "",
      uid: "",
      newType: ""
    }),
    actions: {
      //登录函数
      async login(loginFrom) {
        try {
          const ret = await getLogin(loginFrom);
          const newToken = ret.token;
          setToken(newToken);
          this.token = newToken;
          return ret;
        } catch (error) {
          console.error("登录失败:", error);
          throw error;
        }
      },
      async getLoginUser() {
        // 获取用户信息的方法，可以根据实际API进行实现
      },
      //退出函数
      logout() {
        removeToken();
        this.token = "";
        this.nickName = "";
        this.uid = "";
      },
    },
  }
);